package com.example.zengpengcheng0526;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ListAdapter extends BaseAdapter {
    List<ShangPin> splist;
    LayoutInflater inflater;

    public ListAdapter(List<ShangPin> splist, LayoutInflater inflater){
        this.splist=splist;
        this.inflater=inflater;
    }

    @Override
    public int getCount() {
        return splist!=null?splist.size():0;
    }

    @Override
    public Object getItem(int position) {
        return splist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder{
        ImageView img;
        TextView name;
        TextView jianjie;
        TextView pricee;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh=null;
        if (convertView==null){
            vh=new ViewHolder();
            convertView=inflater.inflate(R.layout.list_item,null);
            vh.img=convertView.findViewById(R.id.img);
            vh.name=convertView.findViewById(R.id.name);
            vh.jianjie=convertView.findViewById(R.id.jianjie);
            vh.pricee=convertView.findViewById(R.id.price);
            convertView.setTag(vh);
        }else {
             vh = (ViewHolder) convertView.getTag();
        }
        ShangPin sp=splist.get(position);
        vh.img.setImageResource(sp.getImg());
        vh.name.setText(sp.getName());
        vh.jianjie.setText(sp.getJianjie());
        vh.pricee.setText(sp.getPrice()+"");
        return convertView;
    }
}
